//
//  CouponViewController.swift
//  DSHCMerchant
//
//  Created by 罗德良 on 2018/4/26.
//  Copyright © 2018年 袋鼠好车. All rights reserved.
//

import UIKit
import Alamofire
import CRRefresh

class CouponViewController: BaseViewController {

    
    @IBOutlet var sureBtn: UIButton!
    @IBOutlet var codeBtn: UITextField!
    @IBOutlet var couponTableview: UITableView!
    
    let couponCell = "couponCell"
//    var storeServices : [ServiceModel]?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        page = 1
        requstCouponList(Page: page!)
        configView()
    }
    
    
    func configView() -> Void {
        couponTableview.delegate = self
        couponTableview.dataSource = self
        couponTableview.separatorStyle = .none
        couponTableview.showsVerticalScrollIndicator = false
        couponTableview.backgroundColor = BaseColor.BackGroundColor
        couponTableview.register(UINib(nibName: "CouponTableViewCell", bundle: nil), forCellReuseIdentifier: couponCell)
//        couponTableview.cr.addHeadRefresh(animator: SlackLoadingAnimator() as CRRefreshProtocol) { [weak self] in
//            self?.requstStoreConfigList()
//        }
    }
    
    
    @IBAction func sureBtnPress(_ sender: UIButton) {
        if codeBtn.text! == "" {
            MBProgressHUD.alertShow(in: self.view, message: "请填写验证码再确认", hidenAfter: 0.8, mode: MBProgressHUDMode.text)
            return
        }
        self.requestCouponVerify(code: codeBtn.text!)
    }
    
    //网络请求
    //优惠券列表
    func requstCouponList(Page:Int) -> Void {
        
        Alamofire.request(couponListUrl, method:.post, parameters: ["storeid":UserModel.shareUser.data?.storeId ?? String(),"page":Page])
            .responseString { response in
                print("fff")
//                self.storeConfigTableview?.cr.endHeaderRefresh()
//                switch response.result.isSuccess {
//                case true:
//
//                    var basemodel = BaseResponseOfModel<BaseArrayData<ServiceModel>>()
//                    basemodel = BaseResponseOfModel.deserialize(from: response.value)!
//
//                    switch basemodel.status! {
//                    case true:
//
//                        self.storeServices = basemodel.data?.data
//                        print("数据请求成功")
//                        self.storeConfigTableview.reloadData()
//                    case false:
//                        print("数据错误")
//
//                    }
//
//                case false:
//                    print(response.result.error!)
//                }
        }
    }
    
    
    //优惠券验证
    func requestCouponVerify(code:String) -> Void {
        Alamofire.request(couponVerifyUrl, method:.post, parameters:["code":code])
            .responseString { response in
                
                switch response.result.isSuccess {
                case true:
                    
                    var basemodel = DefaultData()
                    basemodel = DefaultData.deserialize(from: response.value)!
                    
                    switch basemodel.status! {
                    case true:
                        MBProgressHUD.alertShow(in: self.view, message:"验证成功", hidenAfter: 0.8, mode: MBProgressHUDMode.text)

                        self.codeBtn.text = ""
                    case false:
                         MBProgressHUD.alertShow(in: self.view, message:basemodel.msg, hidenAfter: 0.8, mode: MBProgressHUDMode.text)
                    }
                    
                case false:
                    print(response.result.error!)
                }
                
        }
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    
    /*
     // MARK: - Navigation
     
     // In a storyboard-based application, you will often want to do a little preparation before navigation
     override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
     // Get the new view controller using segue.destinationViewController.
     // Pass the selected object to the new view controller.
     }
     */
    
}


extension CouponViewController: UITableViewDelegate,UITableViewDataSource {
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
//        return self.storeServices?.count ?? 0
        return 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: couponCell, for: indexPath) as! CouponTableViewCell
        cell.selectionStyle = .none
//        let model = self.storeServices![indexPath.item]
//        cell.serviceHeaderLabel.text = model.name
        
        
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return ScaleHeight(90)
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
    }
    //    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    //        return 10
    //    }
    
}
